﻿using System;
using System.Collections.Generic;
using System.Text;

using JGIC.CommandLine.Utility;

using JGICSoft.Tools;
using JGICSoft.Tools.StripperLib;

using System.IO;

using DataTypes = StripperSvc.ASMX.DataTypes;
using ServiceContracts = StripperSvc.ASMX.ServiceContracts;

namespace JGICSoft.Tools
{
    namespace StripperCLiApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                bool DEBUG = true;
                Arguments cli = new Arguments(args);
                bool testsw = false;

                if (cli["test"] != null)
                {
                    testsw = true;

                }

                if (cli["version"] != null | cli["help"] != null | args.Length < 1)
                {
                    Console.WriteLine(@"
File Caracter Stripper version 0.1.0
Copyright (C) 2010 Jean Guillaume Consultant.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Jean Guillaume Isabelle
");
                    if (args.Length < 1 | cli["help"] != null)
                    {
                        Console.WriteLine(@"
HELP--
Usage: Stripper [OPTION...] [FILE]...

--test      print stuff to test (NOT WORKING WELL (it include the --test in filename..)
--version   show version
--help      show this

ie. 
Stripper.exe " + "\"voilà ceci\"" + @"      (will output: " + "voila_ceci" + "\"" + @"
");
                    }
                    System.Threading.Thread.Sleep(2000);
                }
                else
                {


                    Console.Write(GetServiceOutput(args, false, DEBUG));
                    System.Threading.Thread.Sleep(1000);
                    Console.WriteLine("Press any key");
                    Console.Read();
                }

            }

            public static string GetServiceOutput(string[] args, bool testsw, bool DEBUG)
            {
                StringBuilder sb = new StringBuilder(1024);
                using (StringWriter sr = new StringWriter(sb))
                {
                    sr.WriteLine("===========================================");
                    StringBuilder sbsrc = new StringBuilder();
                    foreach (string st in args)
                    {
                        sr.Write(st);
                        sbsrc.Append(st);
                    }
                    sr.WriteLine("\nString fabriqued");
                    try
                    {
                        // Console.WriteLine("\"" + sbsrc + "\" to \"" + sbdest + "\"");
                        if (DEBUG) sr.Write("renaming...");
                        if (!testsw)
                        {
                            if (DEBUG) Console.WriteLine(" if (! testsw)");

                            StripperSvc.ASMX.DataTypes.StripAndRenameResult result = new StripperSvc.ASMX.DataTypes.StripAndRenameResult();
                            StripperSvc.ASMX.DataTypes.MetaData metadata = new StripperSvc.ASMX.DataTypes.MetaData();
                            StripperSvc.ASMX.ServiceContracts.StripAndRenameRequest request = new StripperSvc.ASMX.ServiceContracts.StripAndRenameRequest();
                            StripperSvc.ASMX.ServiceContracts.StripAndRenameResponse response = new StripperSvc.ASMX.ServiceContracts.StripAndRenameResponse();
                            request.FullFilePath = sbsrc.ToString();
                            try
                            {
                                sr.WriteLine("======start RenameAndStripAFileName==");
                                response = Stripper.RenameAndStripAFileName(request);

                                result = response.ResponseResultData;
                                metadata=result.MetaData;
                                sr.WriteLine(response.ResponseMessage);
                                sr.WriteLine("======stop RenameAndStripAFileName==");
                            }
                            catch (Exception ex)
                            {
                                sr.WriteLine(ex.Message);
                            }

                            if (!result.Succeeded)
                            {
                                sr.WriteLine("FALSE returned... that means something wrong...");
                            }
                            //                            File.Move(sbsrc.ToString(), sbdest.ToString()); 
                        }
                        else
                        {
                            if (DEBUG) sr.WriteLine(" else (! testsw)");
                            sr.WriteLine("test mode.");
                            //sr.WriteLine("would be " + sbsrc.ToString() + " to " + sbdest.ToString());
                        }
                        sr.WriteLine("done.");
                    }
                    catch (Exception ex)
                    {
                        sr.WriteLine(ex.Message);
                        //sr.WriteLine("" + sbsrc.ToString() + " to " + sbdest.ToString());
                        sr.WriteLine("did not work...\nsorry");
                    }
                    return sr.ToString();

                }


            }
            // 
        }
    }
}